package com.jinzhi.jzweb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.utils.Result;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzapi.domain.UserDO;
import com.jinzhi.jzweb.domain.JazSysProjectDO;
import com.jinzhi.jzweb.domain.param.UserDemanDQuery;
import com.jinzhi.jzweb.domain.vo.JazUserDemandVO;
import com.jinzhi.jzweb.service.JazSysProjectService;
import com.jinzhi.sys.dao.UserDao;
import org.springframework.stereotype.Service;

import com.jinzhi.jzweb.dao.JazUserDemandDao;
import com.jinzhi.jzweb.domain.JazUserDemandDO;
import com.jinzhi.jzweb.service.JazUserDemandService;
import com.jinzhi.common.base.CoreServiceImpl;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2023-04-18 09:33:30 | Long</small>
 */
@Service
public class JazUserDemandServiceImpl extends CoreServiceImpl<JazUserDemandDao, JazUserDemandDO> implements JazUserDemandService {

    private final JazUserDemandDao jazUserDemandDao;

    private final UserDao userDao;

    /**
     * 项目类型
     */
    private final JazSysProjectService jazSysProjectService;

    public JazUserDemandServiceImpl(JazUserDemandDao jazUserDemandDao, UserDao userDao, JazSysProjectService jazSysProjectService) {
        this.jazUserDemandDao = jazUserDemandDao;
        this.userDao = userDao;
        this.jazSysProjectService = jazSysProjectService;
    }

    @Override
    public JazUserDemandVO selectDemandDetail(Long id) {
        return jazUserDemandDao.selectDemandDetail(id);
    }

    @Override
    public Page<JazUserDemandVO> selectAll(JazUserDemandVO jazUserDemandVO, int current, int size) {
        Page<JazUserDemandVO> page = new Page<>(current, size);
        List<JazUserDemandVO> jazUserOrderVOS = jazUserDemandDao.selectAll(page, jazUserDemandVO);
        return page.setRecords(jazUserOrderVOS);
    }

    /**
     * 多条件分页查询用户需求列表
     * @param userDemanDQuery
     * @return
     */
    @Override
    public Page<JazUserDemandDO> getUserDemandPage(UserDemanDQuery userDemanDQuery) {
        //查询用户需求
        Page<JazUserDemandDO> page = new Page<>(
                Optional.of(userDemanDQuery.getCurr()).orElse(1),
                Optional.of(userDemanDQuery.getPageSize()).orElse(10));
        Wrapper wrapper  = new EntityWrapper().eq(userDemanDQuery.getAuditStatus()!=null,"audit_status",userDemanDQuery.getAuditStatus())
                .orderBy("create_time",false);
        Page<JazUserDemandDO> jdemands =  selectPage(page,wrapper);
        //查询用户信息
        List<Long> userIds = jdemands.getRecords().stream().filter(a ->Objects.nonNull(a.getProjectId())).
                map(JazUserDemandDO::getSysUserid).distinct().collect(Collectors.toList());
        if(!ObjectUtils.isEmpty(userIds)){
            //拼接用户姓名
            List<UserDO> userDOS = userDao.selectList(new EntityWrapper<UserDO>().in("id",userIds).
                    like(StringUtil.notEmpty(userDemanDQuery.getName()),"nickname",userDemanDQuery.getName()));
            if(!ObjectUtils.isEmpty(userDOS)){
                jdemands.getRecords().forEach(a -> userDOS.forEach(b ->{
                    if(Objects.equals(a.getSysUserid(),b.getId())){
                        a.setUserName(b.getNickname());
                        a.setMobile(b.getOrderPhone());
                        a.setName(b.getNickname());
                    }
                }));
            }
        }
        //如果根据姓名查询查询
        if(StringUtil.notEmpty(userDemanDQuery.getName())){
            jdemands.setRecords(jdemands.getRecords().stream().filter(a -> Objects.nonNull(a.getName())).distinct().collect(Collectors.toList()));
        }
        //查询项目类型
        List<Long> projectIds = jdemands.getRecords().stream().filter(a -> Objects.nonNull(a.getProjectId())).
                map(JazUserDemandDO::getProjectId).distinct().collect(Collectors.toList());
        if(ObjectUtils.isEmpty(projectIds)){
            return jdemands;
        }
        List<JazSysProjectDO>  jazSysProjectDOS = jazSysProjectService.selectList(new EntityWrapper<JazSysProjectDO>().in("id",projectIds));
        jdemands.getRecords().forEach(a-> jazSysProjectDOS.forEach(b->{
            if(Objects.nonNull(a.getProjectId())) {
                if (Objects.equals(a.getProjectId(), b.getId())) {
                    a.setProjectName(b.getName());
                }
            }
        }));
        return jdemands;
    }

    @Override
    public JazUserDemandDO getUserDemandById(Long id) {
        JazUserDemandDO jdemand =  selectById(id);
        //查询用户信息
        UserDO userDO = Optional.ofNullable(userDao.selectById(jdemand.getSysUserid())).orElse(new UserDO());
        jdemand.setUserName(userDO.getNickname());
        jdemand.setMobile(userDO.getOrderPhone());
        //查询项目类型
        JazSysProjectDO jazSysProjectDO = Optional.ofNullable(jazSysProjectService.selectById(jdemand.getProjectId())).orElse(new JazSysProjectDO());
        jdemand.setProjectName(jazSysProjectDO.getName());
        return jdemand;
    }
}
